<template>
  <div>
    <h3>分类列表页面</h3>
    <a-card>
      <a-row :gutter="20">
        <a-col :span="4">
          <a-button type="primary" @click="addCate">新增分类</a-button>
        </a-col>
      </a-row>
      <a-table
        rowKey="id"
        :columns="columns"
        :pagination="pagination"
        :dataSource="cateList"
        bordered
        @change="handleTableChange"
      >
        <template slot="action" slot-scope="data">
          <div class="actionSlot">
            <a-button
              icon="edit"
              type="primary"
              style="margin-right:15px"
              @click="editCate(data.id)"
              >编辑</a-button
            >
            <a-button
              icon="delete"
              type="danger"
              style="margin-right:15px"
              @click="deleteCate(data.id)"
              >删除</a-button
            >
          </div>
        </template>
      </a-table>
    </a-card>

    <!-- 新增分类区域 -->
    <a-modal
      closable
      title="新增分类"
      :visible="addCateVisible"
      width="30%"
      @ok="addCateOK"
      @cancel="addCateCancel"
      :destroyOnClose="true"
    >
      <a-form-model :model="newCate" :rules="addCateRules" ref="addCateRef">
        <a-form-model-item has-feedback label="分类名称" prop="name">
          <a-input v-model="newCate.name"></a-input>
        </a-form-model-item>
      </a-form-model>
    </a-modal>

    <!-- 编辑分类区域 -->
    <a-modal
      closable
      title="编辑分类"
      :visible="editCateVisible"
      width="30%"
      @ok="editCateOK"
      @cancel="editCateCancel"
      :destroyOnClose="true"
    >
      <a-form-model :model="cateInfo" :rules="cateRules" ref="editCateRef">
        <a-form-model-item has-feedback label="分类名称" prop="name">
          <a-input v-model="cateInfo.name"></a-input>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </div>
</template>

<script>
const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: '10%',
    key: 'id',
    scopedSlots: { customRender: 'name' },
    align: 'center',
  },
  {
    title: '分类名称',
    dataIndex: 'name',
    width: '20%',
    key: 'name',
    align: 'center',
  },
  {
    title: '操作',
    width: '30%',
    key: 'action',
    scopedSlots: { customRender: 'action' },
    align: 'center',
  },
]

export default {
  data () {
    return {
      pagination: {
        pageSizeOptions: ['5', '10', '20'],
        pageSize: 5,
        total: 0,
        showSizeChanger: true,
        showTotal: total => `共${total}条`,
      },
      cateList: [],
      columns,
      queryParams: {
        pagesize: 5,
        pagenum: 1,
      },
      cateInfo: {
        id: 0,
        name: '',
      },
      newCate: {
        name: '',
      },
      addCateRules: {
        name: [
          { required: true, message: '请输入分类名称！', trigger: 'blur' },
        ],
      },
      cateRules: {
        name: [
          { required: true, message: '请输入分类名称！', trigger: 'blur' },
        ],
      },
      visible: false,
      addCateVisible: false,
      editCateVisible: false,
    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    async getCateList () {
      const { data: res } = await this.$http.get('categorys', {
        params: {
          pagesize: this.queryParams.pagesize,
          pagenum: this.queryParams.pagenum,
        },
      })
      if (res.status !== 200) return this.$message.error(res.message)
      this.cateList = res.data
      this.pagination.total = res.total
    },
    // 更改分页
    handleTableChange (pagination, filters, sorter) {
      const pager = { ...this.pagination }
      pager.current = pagination.current
      pager.pageSize = pagination.pageSize
      this.queryParams.pagesize = pagination.pageSize
      this.queryParams.pagenum = pagination.current

      if (pagination.pageSize !== this.pagination.pageSize) {
        this.queryParams.pagenum = 1
        pager.current = 1
      }
      this.pagination = pager
      this.getCateList()
    },
    // 删除分类
    deleteCate (id) {
      this.$confirm({
        title: '提示：请再次确认！',
        content: '确定要删除该分类吗？一旦删除无法取消！',
        onOk: async () => {
          const { data: res } = await this.$http.delete(`category/${id}`)
          if (res.status !== 200) return this.$message.error(res.message)
          this.$message.success('删除成功！')
          this.getCateList()
        },
        onCancel: () => {
          this.$message.info('已取消删除！')
        },
      })
    },
    // 新增分类按钮
    addCate () {
      this.addCateVisible = true
    },
    // 新增分类
    addCateOK () {
      this.$refs.addCateRef.validate(async valid => {
        if (!valid) return this.$message.error('参数不符合要求，请重新输入！')
        const { data: res } = await this.$http.post('category/add', {
          name: this.newCate.name,
        })
        if (res.status !== 200) return this.$message.error(res.message)
        this.addCateVisible = false
        this.$message.success('分类新增成功！')
        await this.getCateList()
      })
    },
    addCateCancel () {
      this.$refs.addCateRef.resetFields()
      this.addCateVisible = false
      this.$message.info('新增分类已取消！')
    },
    // 编辑分类
    async editCate (id) {
      this.editCateVisible = true
      const { data: res } = await this.$http.get(`category/${id}`)
      this.cateInfo = res.data[0]
      this.cateInfo.id = id
    },

    editCateOK () {
      this.$refs.editCateRef.validate(async valid => {
        if (!valid) return this.$message.error('参数不符合要求，请重新输入！')
        const { data: res } = await this.$http.put(
          `category/${this.cateInfo.id}`,
          {
            name: this.cateInfo.name,
          }
        )
        if (res.status !== 200) return this.$message.error(res.message)
        this.editCateVisible = false
        this.$message.success('更新分类信息成功！')
        this.getCateList()
      })
    },
    editCateCancel () {
      this.$refs.editCateRef.resetFields()
      this.editCateVisible = false
      this.$message.info('编辑已取消！')
    },
  },
}
</script>

<style scoped>
.actionSlot {
  display: flex;
  justify-content: center;
}
</style>
